package com.monkeyboy.algorithem.standard;

import com.monkeyboy.MyThreadLocal;
import com.monkeyboy.config.TestProperties;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import java.math.BigInteger;
import java.util.Collection;
import java.util.Map;

public class MyPreciseTableShardingAlgorithm  implements PreciseShardingAlgorithm<Long> {
    //select * from user where user_id = ? or user_id in (?,?)
    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Long> shardingValue) {
        System.out.println("==============standard");
//        String logicTableName = shardingValue.getLogicTableName();
        String cId = shardingValue.getColumnName();
        Long cIdValue = shardingValue.getValue();
        System.out.println(cId);
        System.out.println(cIdValue);
        //实现 user_0$->{Math.abs(user_id.hashCode())%2+1}
//        BigInteger shardingValueB = BigInteger.valueOf(Math.abs(userIdValue.hashCode())%2+1);
//        BigInteger resB = (shardingValueB.mod(new BigInteger("2"))).add(new BigInteger("1"));
//        String key = logicTableName+"_0"+resB;
//        Map<String,String> table= (Map<String, String>) MyThreadLocal.myThreadLocal.get();
//        System.out.println(table);
//        System.out.println(table.get(cIdValue));
        return "course_2";
//        if(availableTargetNames.contains(key)){
//            return key;
//        }
//        //user_01, course_02
//        throw new UnsupportedOperationException("route "+ key +" is not supported ,please check your config");
    }
}
